package leetcode;

public class 最长回文子串5 {

    public String longestPalindrome(String s) {
        if(s.length() <= 1) {
            return s;
        }
        int max = 0;
        String ano = "";
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j < s.length(); j++) {
                String flag = s.substring(i,j);
                if(judge(flag) && max < flag.length()) {
                    max = j - i;
                    ano = flag;
                }
            }
        }
        return ano;
    }

    /**
     * 判断是否是回文子串，也可以使用栈的特点来实现
     */
    public boolean judge(String str) {
        int length = str.length();
        for (int i = 0; i < length / 2; i++) {
            if(str.charAt(i) != str.charAt(length - i - 1)) {
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args) {

    }
}
